﻿using System;
using System.Collections.Generic;
using System.Text;
using LuaCEGUI.XML;
using LuaCEGUI.Controls;

namespace LuaCEGUI.FormBuilder
{
    class FormBuilder
    {
        Parser p = null;
        public FormBuilder(string name)
        {
            /*
             * XML File Path
            */
            Init(name);
        }

        private int Init(string name)
        {
            p = new Parser(name);
            return 1;
        }

        private int BuildForm(string fname)
        {

            if (ControlManager.AddControl(fname, "Form") == -1) return 0;
            string flocation = p.GetControlAttribute(fname, "Location");
            string[] tmp = flocation.Split(',');
            ControlManager.SetControlLocationByName(fname, System.Convert.ToInt32(tmp[0].ToString()), System.Convert.ToInt32(tmp[1].ToString()));
            string fsize = p.GetControlAttribute(fname, "Size");
            tmp = fsize.Split(',');
            ControlManager.SetControlSizeByName(fname, System.Convert.ToInt32(tmp[0].ToString()), System.Convert.ToInt32(tmp[1].ToString()));
            string ftext = p.GetControlAttribute(fname,"Text");
            ControlManager.SetControlTextByName(fname, ftext);
            return 1;
        }
        private int BuildControl(string cname,string ctype,string fname)
        {
            if (ControlManager.AddControl(cname, ctype) == -1) return 0;
            string clocation = p.GetControlAttribute(cname, "Location");
            if(clocation == null) return 0;
            string[] tmp = clocation.Split(',');
            ControlManager.SetControlLocationByName(cname, System.Convert.ToInt32(tmp[0].ToString()), System.Convert.ToInt32(tmp[1].ToString()));
            string csize = p.GetControlAttribute(cname, "Size");
            if (csize == null) return 0;
            tmp = csize.Split(',');
            ControlManager.SetControlSizeByName(cname, System.Convert.ToInt32(tmp[0].ToString()), System.Convert.ToInt32(tmp[1].ToString()));
            string ctext = p.GetControlAttribute(cname, "Text");
            ControlManager.SetControlTextByName(cname, ctext);

            string eventhandler = p.GetControlAttribute(cname, "EventHandler");
            if (eventhandler != null)
            {
                ControlManager.SetEventHandlerByName(cname, eventhandler);
            }
            else
            {
                ControlManager.SetEventHandlerByName(cname, "DoNothing");
            }

            ControlManager.AddControlToContainerByName(fname, cname);
            return 1;
        }
        private int BuildControlAll(string fname)
        {
            List<string> l = p.GetControlNameList();
            foreach(string name in l)
            {
                string type = p.GetControlAttribute(name,"_ControlType");
                BuildControl(name, type, fname);
            }
            return 1;
        }
        public int RunForm()
        {
            string fname = p.GetFormName();
            BuildForm(fname);
            BuildControlAll(fname);
            ControlManager.RunFormByName(fname);
            return 1;
        }
    }
}
